<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <template #after>
          <el-button type="primary" size="small" @click="addPerm(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <el-table border :data="list" row-key="id">
        <el-table-column align="left" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" @click="addPerm(2,row.id)">新增</el-button>
            <el-button type="text" @click="editPerm(row.id)">编辑</el-button>
            <el-button type="text" @click="onDelPermission(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增权限点 -->
    <el-dialog
      :title="showText"
      :visible="showDialog"
      width="50%"
      @close="btnCancel"
    >
      <el-form ref="permFormRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" style="width:90%" placeholder="请输入权限描述" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确 定</el-button>
          <el-button size="small" @click="btnCancel">取 消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!--

 -->
  </div>
</template>

<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permission.js'
import { tranListToTreeData } from '@/utils/index.js'
export default {
  name: 'PermissionIndex',
  components: {

  },
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
      showDialog: false
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限列表
    async getPermissionList() {
      this.list = tranListToTreeData(await getPermissionList(), '0')
    },
    // 添加权限
    addPerm(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 点击弹层确定的动作(新增成功/编辑成功)
    btnOk() {
      this.$refs.permFormRef.validate(async(isOk) => {
        if (isOk) {
          // formData.id是通过getPermissionDetail()方法返回的值,所以只有在编辑状态下才会有
          if (this.formData.id) {
            // 编辑状态
            await updatePermission(this.formData)
            this.$message.success('编辑成功!')
          } else {
            // 新增状态
            await addPermission(this.formData)
            this.$message.success('添加成功!')
          }
          this.showDialog = false
          this.getPermissionList()
        }
      })
    },
    // 清除数据及验证规则
    btnCancel() {
      this.formData = this.$options.data().formData
      this.showDialog = false
      this.$refs.permFormRef.resetFields()
    },
    // 删除权限
    onDelPermission(row) {
      console.log(row)
      // 通过elmecon
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delPermission(row.id)
        this.getPermissionList()
        // 通过elme
        this.$message({
          message: '删除成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
      }).catch(() => {

      })
    },
    // 获取权限详情以便用于编辑
    async editPerm(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
